home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / ALLOCA.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  5KB  |  177 lines

  1. #include "jam.h"
  2. /* alloca is only required by the GNU regex code */
  3. #ifdef REGEX
  4.  
  5. /*
  6.     alloca -- (mostly) portable public-domain implementation
  7.  
  8.     This implementation of the PWB library alloca() function,
  9.     which is used to allocate space off the run-time stack so
  10.     that it is automatically reclaimed upon procedure exit, 
  11.     was inspired by discussions with J. Q. Johnson of Cornell.
  12.  
  13.     It should work under any C implementation that uses an
  14.     actual procedure stack (as opposed to a linked list of
  15.     frames).  There are some preprocessor constants that can
  16.     be defined when compiling for your specific system, for
  17.     improved efficiency; however, the defaults should be okay.
  18.  
  19.     The general concept of this implementation is to keep
  20.     track of all alloca()-allocated blocks, and reclaim any
  21.     that are found to be deeper in the stack than the current
  22.     invocation.  This heuristic does not reclaim storage as
  23.     soon as it becomes invalid, but it will do so eventually.
  24.  
  25.     As a special case, alloca(0) reclaims storage without
  26.     allocating any.  It is a good idea to use alloca(0) in
  27.     your main control loop, etc. to force garbage collection.
  28. */
  29. #ifndef lint
  30. static char    SCCSid[] = "@(#)alloca.c 1.1";    /* for the "what" utility */
  31. #endif
  32.  
  33. #ifdef X3J11
  34. typedef void    *pointer;        /* generic pointer type */
  35. #else
  36. typedef char    *pointer;        /* generic pointer type */
  37. #endif
  38.  
  39. #define    NULL    0            /* null pointer constant */
  40.  
  41. extern void    free();
  42. extern pointer    malloc();
  43.  
  44. /*
  45.     Define STACK_DIRECTION if you know the direction of stack
  46.     growth for your system; otherwise it will be automatically
  47.     deduced at run-time.
  48.  
  49.     STACK_DIRECTION > 0 => grows toward higher addresses
  50.     STACK_DIRECTION < 0 => grows toward lower addresses
  51.     STACK_DIRECTION = 0 => direction of growth unknown
  52. */
  53.  
  54. #ifndef STACK_DIRECTION
  55. # define    STACK_DIRECTION    0        /* direction unknown */
  56. #endif
  57.  
  58. #if STACK_DIRECTION != 0
  59.  
  60. #define    STACK_DIR STACK_DIRECTION    /* known at compile-time */
  61. #else                    /* STACK_DIRECTION == 0;
  62.                                          * need run-time code 
  63.                                          */
  64. static int    stack_dir = 0;        /* 1 or -1 once known */
  65.  
  66. #define    STACK_DIR    stack_dir
  67.  
  68. static void find_stack_direction( /* void */ )
  69. {
  70.   static char    *addr = NULL;    /* address of first
  71.                    `dummy', once known */
  72.   auto char    dummy;        /* to get stack address */
  73.  
  74.   if ( addr == NULL )
  75.     {            /* initial entry */
  76.       addr = &dummy;
  77.       find_stack_direction();    /* recurse once */
  78.     }
  79.   else                /* second entry */
  80.     if ( &dummy > addr )
  81.       stack_dir = 1;    /* stack grew upward */
  82.     else
  83.       stack_dir = -1;    /* stack grew downward */
  84. }
  85.  
  86. #endif    /* STACK_DIRECTION == 0 */
  87.  
  88. /*
  89.     An "alloca header" is used to:
  90.     (a) chain together all alloca()ed blocks;
  91.     (b) keep track of stack depth.
  92.  
  93.     It is very important that sizeof(header) agree with malloc()
  94.     alignment chunk size.  The following default should work okay.
  95. */
  96.  
  97. #ifndef    ALIGN_SIZE
  98. # define    ALIGN_SIZE    sizeof(double)
  99. #endif
  100.  
  101. typedef union hdr
  102. {
  103.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  104.   struct
  105.     {
  106.       union hdr    *next;    /* for chaining headers */
  107.       char *deep;    /* for stack depth measure */
  108.     }    h;
  109. }    header;
  110.  
  111. /*
  112.     alloca( size ) returns a pointer to at least `size' bytes of
  113.     storage which will be automatically reclaimed upon exit from
  114.     the procedure that called alloca().  Originally, this space
  115.     was supposed to be taken from the current stack frame of the
  116.     caller, but that method cannot be made to work for some
  117.     implementations of C, for example under Gould's UTX/32.
  118. */
  119.  
  120. pointer alloca( size )            /* returns pointer to storage */
  121. unsigned    size;        /* # bytes to allocate */
  122. {
  123.   static header    *last = NULL;    /* -> last alloca header */
  124.   auto char    probe;        /* probes stack depth: */
  125.   register char    *depth = &probe;
  126.  
  127. #if STACK_DIRECTION == 0
  128.   if ( STACK_DIR == 0 )        /* unknown growth direction */
  129.     find_stack_direction();
  130. #endif
  131.  
  132.   /* Reclaim garbage, defined as all alloca()ed storage that
  133.     was allocated from deeper in the stack than currently. */
  134.  
  135.   {
  136.   register header    *hp;        /* traverses linked list */
  137.  
  138.   for ( hp = last; hp != NULL; )
  139.     if ((STACK_DIR > 0) && (hp->h.deep > depth)  
  140.          || (STACK_DIR < 0) && (hp->h.deep < depth))
  141.       {
  142.         register header    *np = hp->h.next;
  143.  
  144.         free((pointer)hp);        /* collect garbage */
  145.         hp = np;            /* -> next header */
  146.       }
  147.     else
  148.       break;            /* rest are not deeper */
  149.  
  150.     last = hp;            /* -> last valid storage */
  151.   }
  152.  
  153.   if ( size == 0 )
  154.     return NULL;        /* no allocation required */
  155.  
  156.   /* Allocate combined header + user data storage. 
  157.   */
  158.   {
  159.     register pointer new = malloc( sizeof(header) + size );
  160.                     /* address of header */
  161.  
  162.     if ( new == NULL )
  163.     return NULL;        /* abort() is traditional */
  164.  
  165.     ((header *)new)->h.next = last;
  166.     ((header *)new)->h.deep = depth;
  167.  
  168.     last = (header *)new;
  169.  
  170.     /* User storage begins just after header. */
  171.  
  172.     return (pointer)((char *)new + sizeof(header));
  173.   }
  174. }
  175. #endif
  176.  
  177.